<?php
class BrandModel extends Model{
	public $table = 'brand';
	public $validate=array(
			array('bname','nonull','品牌名不能为空',2,3)
		);
	public $auto=array(
			array('logo','upLogo','method',2,3),
		);
	public function upLogo(){
		$upload = new upload();
		$oldpic = Q('oldpic');
		if(!empty($_FILES['logo']['name'])&&!empty($oldpic)){
			unlink($oldpic);
			$info = $upload->upload('logo');
			return $info[0]['path'];

		}else if(!empty($oldpic)){
			return $oldpic;
		}else{
			$info = $upload->upload('logo');
			return $info[0]['path'];
		}
	}
	public function addBrand(){
		if($this->create()){
			if($this->add()){
				$this->updateCache();
				return true;
			}else{
				$this->error = '品牌添加失败';
				return false;
			}
		}
	}
	public function saveBrand($bid){
		if($this->create()){
			if($this->where(array('bid'=>$bid))->save()){
				$this->updateCache();
				return true;
			}else{
				$this->error = '品牌更新失败';
				return false;
			}
		}
	}
	public function updateCache(){
		$data = $this->all();
		$cache = array();
		foreach ($data as $key => $d) {
			$cache[$d['bid']] = $d;
		}
		return catched('brand',$cache);
	}
}
?>
